package com.kakame.codenews.base.util;

import java.security.MessageDigest;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

/**
 * <pre>
 * <b>desc:</b> 用户密码加密算法 
 * <b>auth:</b> 何家明
 * <b>date:</b> 2018年6月6日 下午3:21:17
 * </pre>
 */
public class EncryptUtil {

    private static final String[] HEX_DIGITS = { "0", "1", "2", "3", "4", "5",
            "6", "!", "#", "@", "a", "b", "c", "d", "*", "f", "g", "F" };

    private static final String ALGORITHM = "sha-256";

    /**
     * <pre>
     * <b>desc:</b> 加密 
     * <b>auth:</b> 何家明
     * <b>date:</b> 2018年6月6日 下午3:21:59
     * </pre>
     * 
     * @param password
     *            明文密码
     * @param salt
     *            盐值
     * @return
     */
    public static String encrypt(String password, Object salt) {
        String result = null;
        try {
            MessageDigest md = MessageDigest.getInstance(ALGORITHM);
            // 加密后的字符串
            if (password == null) {
                password = "";
            }
            if (salt != null) {
                password = password + "{" + salt.toString() + "}";
            }
            byte[] digest = md.digest(password.getBytes("utf-8"));
            StringBuilder resultSb = new StringBuilder();
            for (int i = 0; i < digest.length; i++) {
                resultSb.append(byteToHexString(digest[i]));
            }
            return resultSb.toString();
        } catch (Exception ex) {
        }
        return result;
    }

    /**
     * <pre>
     * <b>desc:</b> 密码比对 
     * <b>auth:</b> 何家明
     * <b>date:</b> 2018年6月6日 下午3:23:03
     * </pre>
     * 
     * @param password
     *            明文密码
     * @param dbPassword
     *            加密后的密码
     * @param salt
     *            盐值
     * @return
     */
    public static boolean compare(String password, Object dbPassword,
            Object salt) {
        return StringUtils.equals(encrypt(password, salt), String.valueOf(dbPassword));
    }

    /**
     * <pre>
     * <b>desc:</b> 生成随机盐 
     * <b>auth:</b> 何家明
     * <b>date:</b> 2018年6月6日 下午3:25:28
     * </pre>
     * 
     * @return
     */
    public static String salt() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * <pre>
     * <b>desc:</b> 转换字节为16进制字串
     * <b>auth:</b> 何家明
     * <b>date:</b> 2018年6月6日 下午3:30:20
     * </pre>
     * 
     * @param b
     * @return
     */
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int d1 = n / HEX_DIGITS.length;
        int d2 = n % HEX_DIGITS.length;
        return HEX_DIGITS[d1] + HEX_DIGITS[d2];
    }
    
    public static void main(String[] args) {
        String salt = salt();
        System.out.println("salt: " + salt + ", length: " + salt.length());
        String password = "1qaz2wsx";
        String encrypt = encrypt(password, salt);
        System.out.println("encrypt: " + encrypt + ", length: " + encrypt.length());
        System.out.println(compare(password, "@a05#f61cg56!adg4#4b614c!162c2@66@@a#6ab4@5@!2!fc!4g3f1bd4b0#d2!", "65d4521b90af4e278b772798c592870d"));
    }
}
